/*
 * CanvasAnimate.java
 *
 * Created on 4 de Junho de 2007, 23:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view.components;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author $Leandro$
 */
public class CanvasAnimate extends CanvasAbstract implements Runnable {
    
    private int imgIndex = 0;
    
    private Vector imgList;
    
    private int imgListSize;
    
    private int align;
    
    public CanvasAnimate(Graphics g, Vector imgPaths,int x, int y, int align) {
        super(g);
        try {
            this.x = x;
            this.y = y;
            this.align = align;
            
            imgList = new Vector();
            
            for(int i = 0; i < imgPaths.size(); i++) {
                imgList.addElement(Image.createImage((String) imgPaths.elementAt(i)));
            }
            
            imgListSize = imgList.size();
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void paint() {
        g.drawImage((Image) imgList.elementAt(imgIndex), 0,0,g.TOP|g.LEFT);
    }
    
    public void startAnimation(){
        Thread th=new Thread(this);
        th.start();        
    }
    
    public void run() {
        while(true) {            
            imgIndex++;
            imgIndex%=imgListSize;
            paint();
            try {
                Thread.sleep(100);
            } catch(Exception e){}
        }
    }
}
